import { post } from "./request";

// 类型定义
export interface LoginParams {
  username: string;
  password: string;
  remember?: boolean;
}

export interface PhoneLoginParams {
  phone: string;
  code: string;
}

export interface RegisterParams {
  username: string;
  email?: string;
  phone?: string;
  password: string;
  code?: string;
  captcha?: string;
}

export interface ResetPasswordParams {
  account: string;
  code: string;
  newPassword: string;
}

export interface UserInfo {
  id: string;
  username: string;
  email?: string;
  phone?: string;
  avatar?: string;
  role: "student" | "teacher" | "admin";
  createdAt?: string;
}

export interface AuthResponse {
  token: string;
  user: UserInfo;
}

// 账号密码登录
export const login = (params: LoginParams): Promise<AuthResponse> => {
  return post("/auth/login", params);
};

// 手机号登录
export const phoneLogin = (params: PhoneLoginParams): Promise<AuthResponse> => {
  return post("/auth/phone-login", params);
};

// 邮箱注册
export const registerByEmail = (
  params: RegisterParams
): Promise<AuthResponse> => {
  return post("/auth/register/email", params);
};

// 手机号注册
export const registerByPhone = (
  params: RegisterParams
): Promise<AuthResponse> => {
  return post("/auth/register/phone", params);
};

// 发送邮箱验证码
export const sendEmailCode = (email: string): Promise<void> => {
  return post("/auth/send-email-code", { email });
};

// 发送手机验证码
export const sendPhoneCode = (phone: string): Promise<void> => {
  return post("/auth/send-phone-code", { phone });
};

// 验证账号（找回密码第一步）
export const verifyAccount = (account: string): Promise<void> => {
  return post("/auth/verify-account", { account });
};

// 验证验证码（找回密码第二步）
export const verifyCode = (account: string, code: string): Promise<void> => {
  return post("/auth/verify-code", { account, code });
};

// 重置密码
export const resetPassword = (params: ResetPasswordParams): Promise<void> => {
  return post("/auth/reset-password", params);
};

// 登出
export const logout = (): Promise<void> => {
  return post("/auth/logout");
};

// 获取当前用户信息
export const getCurrentUser = (): Promise<UserInfo> => {
  return post("/auth/current-user");
};

// 刷新token
export const refreshToken = (): Promise<{ token: string }> => {
  return post("/auth/refresh-token");
};

// 验证token是否有效
export const validateToken = (): Promise<boolean> => {
  return post("/auth/validate-token");
};
